import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 任务管理系统 - 分支功能：删除任务
 * 功能：
 * 1. 按ID删除任务
 * 2. 更新分类和标签映射
 */
public class TaskDeletionManager {

    // 任务类
    private static class Task {
        private int id;
        private String description;
        private String status;
        private String priority;
        private String category;
        private List<String> tags;

        public Task(int id, String description, String status, String priority, String category) {
            this.id = id;
            this.description = description;
            this.status = status;
            this.priority = priority;
            this.category = category;
            this.tags = new ArrayList<>();
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        public String getStatus() {
            return status;
        }

        public String getPriority() {
            return priority;
        }

        public String getCategory() {
            return category;
        }

        public List<String> getTags() {
            return tags;
        }

        public void addTag(String tag) {
            this.tags.add(tag);
        }

        @Override
        public String toString() {
            return "Task{" +
                    "id=" + id +
                    ", description='" + description + '\'' +
                    ", status='" + status + '\'' +
                    ", priority='" + priority + '\'' +
                    ", category='" + category + '\'' +
                    ", tags=" + tags +
                    '}';
        }
    }

    // 删除管理器类
    private static class DeletionManager {
        private List<Task> tasks;
        private Map<String, List<Task>> categoryMap;
        private Map<String, List<Task>> tagMap;

        public DeletionManager(List<Task> tasks) {
            this.tasks = tasks;
            this.categoryMap = new HashMap<>();
            this.tagMap = new HashMap<>();
            initializeMaps();
        }

        private void initializeMaps() {
            for (Task task : tasks) {
                // 初始化分类映射
                categoryMap.computeIfAbsent(task.getCategory(), k -> new ArrayList<>()).add(task);

                // 初始化标签映射
                for (String tag : task.getTags()) {
                    tagMap.computeIfAbsent(tag, k -> new ArrayList<>()).add(task);
                }
            }
        }

        public boolean deleteTaskById(int id) {
            Task taskToDelete = null;
            for (Task task : tasks) {
                if (task.getId() == id) {
                    taskToDelete = task;
                    break;
                }
            }

            if (taskToDelete == null) {
                return false;
            }

            // 从任务列表中移除
            tasks.remove(taskToDelete);

            // 更新分类映射
            categoryMap.get(taskToDelete.getCategory()).remove(taskToDelete);
            if (categoryMap.get(taskToDelete.getCategory()).isEmpty()) {
                categoryMap.remove(taskToDelete.getCategory());
            }

            // 更新标签映射
            for (String tag : taskToDelete.getTags()) {
                tagMap.get(tag).remove(taskToDelete);
                if (tagMap.get(tag).isEmpty()) {
                    tagMap.remove(tag);
                }
            }

            return true;
        }

        public List<Task> getAllTasks() {
            return new ArrayList<>(tasks);
        }
    }

    // 主类
    public static void main(String[] args) {
        List<Task> tasks = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int nextId = 1;

        // 示例任务数据
        Task task1 = new Task(nextId++, "完成项目文档", "进行中", "高", "文档");
        task1.addTag("紧急");
        task1.addTag("文档");

        Task task2 = new Task(nextId++, "修复登录模块的Bug", "未完成", "中", "Bug修复");
        task2.addTag("Bug");
        task2.addTag("登录");

        Task task3 = new Task(nextId++, "设计用户界面", "完成", "高", "设计");
        task3.addTag("UI");
        task3.addTag("设计");

        tasks.add(task1);
        tasks.add(task2);
        tasks.add(task3);

        DeletionManager deletionManager = new DeletionManager(tasks);

        while (true) {
            System.out.println("\n===== 任务删除系统 =====");
            System.out.println("1. 删除任务");
            System.out.println("2. 显示所有任务");
            System.out.println("0. 退出");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入要删除的任务ID: ");
                    int taskId = scanner.nextInt();
                    scanner.nextLine(); // 消耗换行符

                    if (deletionManager.deleteTaskById(taskId)) {
                        System.out.println("任务ID为 " + taskId + " 的任务已成功删除");
                    } else {
                        System.out.println("未找到ID为 " + taskId + " 的任务");
                    }
                    break;
                case 2:
                    List<Task> allTasks = deletionManager.getAllTasks();
                    if (allTasks.isEmpty()) {
                        System.out.println("当前没有任务");
                    } else {
                        System.out.println("\n===== 所有任务 =====");
                        for (Task task : allTasks) {
                            System.out.println(task);
                        }
                    }
                    break;
                case 0:
                    System.out.println("退出系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选择，请重新输入");
            }
        }
    }
}